package com.oanda.blackberry.view;

import net.rim.device.api.ui.Field;

import com.oanda.blackberry.restapi.PricePoint;

public abstract class AbstractChartField extends Field {
    protected String symbol;
    protected int precious;
    protected PricePoint[] points;
    protected int width = 460;

    protected int height = 200;

    protected static int chartType = 0;
    public static final int MAX_MIN_RIBBON = 0;
    public static final int CANDLESTICK = 1;
    public static final int MAXBID = 2;
    public static final int MINBID = 3;
    protected static int PRICE_TYPE = 1;
    protected static final int PRICE_MINBID = 1;
    protected static final int PRICE_MINASK = 2;
    protected static final int PRICE_MAXBID = 3;
    protected static final int PRICE_MAXASK = 4;
    protected static final int PRICE_OPENBID = 5;
    protected static final int PRICE_OPENASK = 6;
    protected static final int PRICE_CLOSEBID = 7;
    protected static final int PRICE_CLOSEASK = 8;
    protected static final int PRICE_MIN_MAX_MIN_RIBBON = 9;
    protected static final int PRICE_MAX_MAX_MIN_RIBBON = 10;
    protected static final int PRICE_CANDLESTICK_MIN = 11;
    protected static final int PRICE_CANDLESTICK_MAX = 12;

    public void setGraphData(PricePoint[] points) {
        this.points = points;
    }

    public int getPreferredHeight() {
        return this.height;
    }

    public int getPreferredWidth() {
        return this.width;
    }

    protected void layout(int width, int height) {
        int w;
        if (width >= this.width)
            w = this.width;
        else
            w = width;
        int h;
        if (height >= this.height)
            h = this.height;
        else {
            h = height;
        }

        setExtent(w, h);
    }

    protected float getMin(int priceTypeee) {
        float min = 0.0F;
        float minPriceTemp = 0.0F;
        switch (priceTypeee) {
        case 11:

            min = this.points[0].getMinPriceAvr();

            for (int i = 0; i < this.points.length; i++) {
                minPriceTemp = this.points[i].getMinPriceAvr();
                if (minPriceTemp < min) {
                    min = minPriceTemp;
                }
            }
            break;
        case 9:

            min = Math.min(this.points[0].minAsk, this.points[0].minBid);

            for (int i = 0; i < this.points.length; i++) {
                minPriceTemp = Math.min(this.points[i].minAsk, this.points[i].minBid);
                if (minPriceTemp < min) {
                    min = minPriceTemp;
                }
            }
            break;
        case 12:

            min = this.points[0].getMaxPriceAvr();

            for (int i = 0; i < this.points.length; i++) {
                minPriceTemp = this.points[i].getMaxPriceAvr();
                if (minPriceTemp < min)
                    min = minPriceTemp;
            }
            break;
        case 10:

            min = Math.min(this.points[0].maxAsk, this.points[0].maxBid);

            for (int i = 0; i < this.points.length; i++) {
                minPriceTemp = Math.min(this.points[i].maxAsk, this.points[i].maxBid);
                if (minPriceTemp < min) {
                    min = minPriceTemp;
                }
            }
            break;
        case 1:

            min = this.points[0].minBid;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].minBid < min) {
                    min = this.points[i].minBid;
                }
            }
            break;
        case 2:

            min = this.points[0].minAsk;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].minAsk < min) {
                    min = this.points[i].minAsk;
                }
            }
            break;
        case 3:

            min = this.points[0].maxBid;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].maxBid < min) {
                    min = this.points[i].maxBid;
                }
            }
            break;
        case 4:

            min = this.points[0].maxAsk;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].maxAsk < min) {
                    min = this.points[i].maxAsk;
                }
            }
            break;
        case 5:

            min = this.points[0].openBid;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].openBid < min) {
                    min = this.points[i].openBid;
                }
            }
            break;
        case 6:

            min = this.points[0].openAsk;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].openAsk < min) {
                    min = this.points[i].openAsk;
                }
            }
            break;
        case 7:

            min = this.points[0].closeBid;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].closeBid < min) {
                    min = this.points[i].closeBid;
                }
            }
            break;
        case 8:

            min = this.points[0].closeAsk;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].closeAsk < min) {
                    min = this.points[i].closeAsk;
                }
            }

        }

        return min;
    }

    protected float getMinMinBid() {
        float min = this.points[0].minBid;
        for (int i = 0; i < this.points.length; i++) {
            if (this.points[i].minBid < min) {
                min = this.points[i].minBid;
            }
        }
        return min;
    }

    protected float getMax(int priceType) {
        float max = 0.0F;
        float maxPriceTemp = 0.0F;
        switch (priceType) {
        case 11:

            max = this.points[0].getMinPriceAvr();

            for (int i = 0; i < this.points.length; i++) {
                maxPriceTemp = this.points[i].getMinPriceAvr();
                if (maxPriceTemp > max) {
                    max = maxPriceTemp;
                }
            }
            break;
        case 9:

            max = Math.max(this.points[0].minAsk, this.points[0].minBid);

            for (int i = 0; i < this.points.length; i++) {
                maxPriceTemp = Math.max(this.points[i].minAsk, this.points[i].minBid);
                if (maxPriceTemp > max) {
                    max = maxPriceTemp;
                }
            }
            break;
        case 10:

            max = Math.max(this.points[0].maxAsk, this.points[0].maxBid);

            float maxPrice2 = 0.0F;

            for (int i = 0; i < this.points.length; i++) {
                maxPrice2 = Math.max(this.points[i].maxAsk, this.points[i].maxBid);
                if (maxPrice2 > max) {
                    max = maxPrice2;
                }
            }
            break;
        case 12:

            max = this.points[0].getMaxPriceAvr();

            for (int i = 0; i < this.points.length; i++) {
                maxPriceTemp = this.points[i].getMaxPriceAvr();
                if (maxPriceTemp > max) {
                    max = maxPriceTemp;
                }
            }
            break;
        case 8:

            max = this.points[0].closeAsk;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].closeAsk > max) {
                    max = this.points[i].closeAsk;
                }
            }
            break;
        case 7:

            max = this.points[0].closeBid;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].closeBid > max) {
                    max = this.points[i].closeBid;
                }
            }
            break;
        case 6:

            max = this.points[0].openAsk;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].openAsk > max) {
                    max = this.points[i].openAsk;
                }
            }
            break;
        case 5:

            max = this.points[0].openBid;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].openBid > max) {
                    max = this.points[i].openBid;
                }
            }
            break;
        case 4:

            max = this.points[0].maxAsk;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].maxAsk > max) {
                    max = this.points[i].maxAsk;
                }
            }
            break;
        case 3:

            max = this.points[0].maxBid;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].maxBid > max) {
                    max = this.points[i].maxBid;
                }
            }
            break;
        case 2:

            max = this.points[0].minAsk;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].minAsk > max) {
                    max = this.points[i].minAsk;
                }
            }
            break;
        case 1:

            max = this.points[0].minBid;

            for (int i = 0; i < this.points.length; i++) {
                if (this.points[i].minBid > max) {
                    max = this.points[i].minBid;
                }

            }

        }

        return max;
    }

    protected long getMaxTime() {
        long max = this.points[0].time;
        for (int i = 0; i < this.points.length; i++) {
            if (this.points[i].time > max) {
                max = this.points[i].time;
            }
        }
        return max;
    }

    protected long getMinTime() {
        long min = this.points[0].time;
        for (int i = 0; i < this.points.length; i++) {
            if (this.points[i].time < min) {
                min = this.points[i].time;
            }
        }
        return min;
    }
}
